class BG3PlayerController extends UTPlayerController
	dependson(BG3Pawn)
	config(Game);

enum EMouseEvent
{
	LeftMouseButton,
	RightMouseButton,
	MiddleMouseButton,
	ScrollWheelUp,
	ScrollWheelDown,
};

//store mouse coordinates
var IntPoint CurrentMousePosition;

//overwritten to detect mouseclick
exec function StartFire( optional byte FireModeNum )
{
	HandleMouseInput((FireModeNum == 0) ? LeftMouseButton : RightMouseButton, IE_Pressed);
	Super.StartFire(FireModeNum);
}

exec function StopFire( optional byte FireModeNum )
{
	HandleMouseInput((FireModeNum == 0) ? LeftMouseButton : RightMouseButton, IE_Released);
	Super.StopFire(FireModeNum);
}

//overwritten to enable suicides in multiplayer to work reliably
reliable server function ServerSuicide()
{
	if ( (Pawn != None) || (WorldInfo.NetMode == NM_Standalone))
	{
		Pawn.Suicide();
	}
}

//handle mouse input
function HandleMouseInput(EMouseEvent MouseEvent, EInputEvent InputEvent)
{
	local int i;
	if(InputEvent == IE_Pressed)
	{
		//do nothing for now, we just want to released event
		return;
	}
	else if(InputEvent == IE_Released)
	{
		switch(MouseEvent)
		{
			case LeftMouseButton:
			if(BG3HUD(myHUD) != none)
			{
				if(BG3HUD(myHUD).currhudlayout.bEnabled == true && BG3HUD(myHUD).currhudlayout.bShowMouse == true)
				{
					if(BG3HUD(myHUD).currhudlayout.mouseoverelements.Length > 0)
					{
						for(i=0;i<BG3HUD(myHUD).currhudlayout.mouseoverelements.Length;i++)
						{
							BG3HUD(myHUD).currhudlayout.mouseoverelements[i].OnLeftPressed(self);
						}
					}
				}
			}
			break;

			//we want nothing for rightmouse button yet
			case RightMouseButton:
			break;

			default:
			break;
		}
	}
}


//overwritten to prevent mouselook when a HUDlayer with bShowMouse is enabled
function UpdateRotation( float DeltaTime )
{
	local Rotator	DeltaRot, newRotation, ViewRotation;

	ViewRotation = Rotation;
	if (Pawn!=none)
	{
		Pawn.SetDesiredRotation(ViewRotation);
	}

	// Calculate Delta to be applied on ViewRotation
	//figure out if we enable mouselook or not (based on current hudlayout)
	if(BG3HUD(myHUD) != none)
	{
		if(BG3HUD(myHUD).currhudlayout != none)
		{
			if(BG3HUD(myHUD).currhudlayout.bEnabled && BG3HUD(myHUD).currhudlayout.bShowMouse)
			{	
				//if all true, skip calcrotation entirely and return
				return;
			}
		}
	}
	DeltaRot.Yaw	= PlayerInput.aTurn;
	DeltaRot.Pitch	= PlayerInput.aLookUp;
	ProcessViewRotation( DeltaTime, ViewRotation, DeltaRot );
	SetRotation(ViewRotation);

	ViewShake( deltaTime );

	NewRotation = ViewRotation;
	NewRotation.Roll = Rotation.Roll;

	if ( Pawn != None )
		Pawn.FaceRotation(NewRotation, deltatime);
}

// Override this state because StartFire isn't called globally when in this function
auto state PlayerWaiting
{
	exec function StartFire(optional byte FireModeNum)
	{
		Global.StartFire(FireModeNum);
	}
}

defaultproperties
{
	CameraClass = class'BG3Game.BG3Camera'
	InputClass = class'BG3Game.BG3PlayerInput'

	FOVAngle = 90.0f
	DesiredFOV = 90.0f
	DefaultFOV = 90.0f
	bRun = 1

	//bSimpleCrosshair = true;
	//bNoCrosshair = false;
}